package com.rivulet.foreign.mapper;

import com.rivulet.foreign.pojo.User;
import com.rivulet.foreign.pojo.UserInformation;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * {用户表和用户信息表的数据持久层}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Component
public interface UserMapper {

    /**
     * 根据学工号user_id查询用户账户
     *
     * @param user_id
     * @return
     */
    @Select("select * from t_user where user_id=#{user_id}")
    public User getUserById(@Param("user_id") String user_id);

    /**
     * 获得所有用户信息
     *
     * @return
     */
    @Select("select * from t_user")
    public List<User> getUsers();

    /**
     * 获得与关键词id模糊相像的所有用户
     *
     * @param id
     * @return
     */
    @Select("select * from t_user where user_id LIKE CONCAT('%',#{id},'%') ")
    public List<User> getUsersLikeId(@Param("id")String id);

    /**
     * 获得指定身份的所有用户
     *
     * @param identity
     * @return
     */
    @Select("select * from t_user where identity=#{identity}")
    public List<User> getUsersLikeIdentity(@Param("identity")Integer identity);

    /**
     * 获得限制条件的用户
     *
     * @param id
     * @param identity
     * @return
     */
    @Select("select * from t_user where user_id LIKE CONCAT('%',#{id},'%') AND identity=#{identity}")
    public List<User> getUsersLike(@Param("id")String id,@Param("identity")Integer identity);
    /**
     * 根据user_id查询用户身份identity
     *
     * @param user_id
     * @return
     */
    @Select("select identity from t_user where user_id=#{user_id}")
    public Integer getIdentityById(@Param("user_id") String user_id);

    /**
     * 插入一条用户数据
     *
     * @param user_id
     * @param password
     * @param identity
     */
    @Insert("insert into t_user (user_id,password,identity) values (#{user_id},#{password},#{identity})")
    public void setUser(@Param("user_id") String user_id, @Param("password") String password, @Param("identity") Integer identity);

    /**
     * 删除指定user_id的用户数据
     *
     * @param user_id
     */
    @Delete("delete from t_user where user_id=#{user_id}")
    public void delUser(@Param("user_id") String user_id);

    /**
     * 修改用户密码
     *
     * @param password
     * @param user_id
     */
    @Update("update t_user set password=#{password} where user_id=#{user_id}")
    public void reSetPassword(@Param("password") String password, @Param("user_id") String user_id);

    /**
     * 新增一条空用户信息元组
     *
     * @param user_id
     */
    @Insert("insert into t_user_information (user_id) values (#{user_id})")
    public void setUserInformation(@Param("user_id") String user_id);

    /**
     * 更新指定user_id的用户信息元组
     *
     * @param passport_name
     * @param chinese_name
     * @param student_type
     * @param citizenship
     * @param sex
     * @param birth_date
     * @param dormitory
     * @param classes
     * @param start_school_time
     * @param leave_school_time
     * @param passport_id
     * @param passport_end_time
     * @param visa_id
     * @param visa_end_time
     * @param insurance_end_time
     * @param expenditure_type
     * @param major
     * @param religion
     * @param winning_information
     * @param phone_number
     * @param weChat
     * @param email
     * @param user_id
     */
    @Update("update t_user_information set " +
            "passport_name=#{passport_name}," +
            "chinese_name=#{chinese_name}," +
            "student_type=#{student_type}," +
            "citizenship=#{citizenship}," +
            "sex=#{sex}," +
            "birth_date=#{birth_date}," +
            "dormitory=#{dormitory}," +
            "classes=#{classes}," +
            "start_school_time=#{start_school_time}," +
            "leave_school_time=#{leave_school_time}," +
            "passport_id=#{passport_id}," +
            "passport_end_time=#{passport_end_time}," +
            "visa_id=#{visa_id}," +
            "visa_end_time=#{visa_end_time}," +
            "insurance_end_time=#{insurance_end_time}," +
            "expenditure_type=#{expenditure_type}," +
            "major=#{major}," +
            "religion=#{religion}," +
            "winning_information=#{winning_information}," +
            "phone_number=#{phone_number}," +
            "weChat=#{weChat}," +
            "email=#{email}" +
            "where user_id=#{user_id}")
    public void updateUserInformation(
            @Param("passport_name") String passport_name,
            @Param("chinese_name") String chinese_name,
            @Param("student_type") String student_type,
            @Param("citizenship") String citizenship,
            @Param("sex") Integer sex,
            @Param("birth_date") Date birth_date,
            @Param("dormitory") String dormitory,
            @Param("classes") String classes,
            @Param("start_school_time") Date start_school_time,
            @Param("leave_school_time") Date leave_school_time,
            @Param("passport_id") String passport_id,
            @Param("passport_end_time") Date passport_end_time,
            @Param("visa_id") String visa_id,
            @Param("visa_end_time") Date visa_end_time,
            @Param("insurance_end_time") Date insurance_end_time,
            @Param("expenditure_type") String expenditure_type,
            @Param("major") String major,
            @Param("religion") String religion,
            @Param("winning_information") String winning_information,
            @Param("phone_number") String phone_number,
            @Param("weChat") String weChat,
            @Param("email") String email,
            @Param("user_id") String user_id
    );

    /**
     * 查询所有学生用户的信息
     *
     * @return
     */
    @Select("SELECT * from  t_user_information WHERE user_id IN (SELECT user_id FROM t_user WHERE identity = 1)")
    public List<UserInformation> getUserInformation();

    /**
     * 获得指定学生的班级信息
     *
     * @param user_id
     * @return
     */
    @Select("select classes from t_user_information where user_id=#{user_id}")
    public String getClassesById(@Param("user_id") String user_id);

    /**
     * 获得指定学生的中文姓名
     *
     * @param user_id
     * @return
     */
    @Select("select chinese_name from t_user_information where user_id=#{user_id}")
    public String getNameById(@Param("user_id") String user_id);

    /**
     * 获得指定学号的关联学生信息
     *
     * @param user_id
     * @return
     */
    @Select("select * from t_user_information where user_id=#{user_id}")
    public UserInformation getUserInformationById(@Param("user_id") String user_id);

    /**
     * 删除用户信息
     *
     * @param user_id
     */
    @Delete("delete from t_user_information where user_id=#{user_id}")
    public void deleteUserInformation(@Param("user_id")String user_id);
}
